def print_http_headers(headers):
  for (key, value) in headers.items():
    print("%s: %s" % (key, value))
  print("")

def print_page_open(title, extra=""):
  print('<!DOCTYPE HTML>')
  print('<html>')
  print('<head>')
  print('<title>%s</title>' % title)
  print('<link rel="stylesheet" type="text/css" href="css/screen.css" />')
  print('<link rel="shortcut icon" href="favicon.ico" />')
  print(extra)
  print('</head>')

def print_page_header(menu, user=None):
  print('<div class="clearfix" id="header">')
  print_page_menu(menu, user)
  if user is not None:
    print("""
    <p id="logged_in_status">
      %s <a href="logout.cgi">logout</a>
    </p>
    """ % user["username"]) 
  else:
    print("""
    <p id="logged_in_status">
      <a href="login.cgi">login</a>
      <a href="register.cgi">register</a>
    </p>
    """) 
  print('</div>')

menu_link_ids = {}
menu_link_ids["gallery.cgi"] = "link_gallery"
menu_link_ids["login.cgi"] = "link_login"
menu_link_ids["logout.cgi"] = "link_logout"
menu_link_ids["album_picture_add.cgi"] = "link_albumpictureadd"
menu_link_ids["gallery_admin_user_add.cgi"] = "link_galleryadminuseradd"
def print_page_menu(links, user=None):
  if user is not None:
    links.append(("Create Album", "create_album.cgi"))
#    links.append(("Logout", "logout.cgi"))
    links.append(("Account", "account_settings.cgi"))
    if user["root"] == 1:
      links.append(("User List", "admin_user_list.cgi"))
      links.append(("Add User", "admin_user_add.cgi"))
  else:
    pass
#    links.append(("Login", "login.cgi"))
  print('<ul id="main_menu">')
  for link in links:
    try:
      id = menu_link_ids[link[1]]
    except KeyError:
      id = ""
    print('<li id="%s"><a href="%s">%s</a></li>' 
        % (id, link[1], link[0]))
  print('</ul>')

def print_page_close():
  print('</body>')
  print('</html>')

def print_file(file):
  f = open(file, "r")
  s = f.read()
  print(s)

"""
Very simple templating
Loads file and replaces tokens in the file with values in param
"""
def print_template(file, param):
  f = open(file, "r")
  s = f.read()
  for key in param:
    s = s.replace("{%s}" % key, param[key])
  print(s)
